﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;

using VideoPlay.Services;
using VideoPlay.Services.Models;
using VideoPlay.Adapters;

namespace VideoPlay.PhoneClient.ViewModels
{
    public class VideoListViewModel : ViewModel
    {
        public VideoListViewModel(
            INavigationService navigationService,
            IPhoneService phoneService,
            IndicatorViewModel indicator)
            : base(navigationService, phoneService,indicator, new Uri(@"/Views/VideoListView.xaml", UriKind.Relative))
        {
            LoadVideosFromService();
        }

        private void LoadVideosFromService()
        {
            Indicator.Show();

            this.PhoneServiceFacade.RetrieveCategoriesAsync(OnReceivedCategories);
        }

        private void OnReceivedCategories(object param)
        {
            List<Categories> categories = param as List<Categories>;

            ObservableCollection<VideoCategoryViewModel> tabs = new ObservableCollection<VideoCategoryViewModel>();
            foreach (var category in categories)
            {
                VideoCategoryViewModel tab = new VideoCategoryViewModel(NavigationService,PhoneServiceFacade, Indicator);
                tab.VideoCategory = category.Category;
                tab.ItemName = category.Category.name;

                tabs.Add(tab);
            }

            this.CategoryItems = tabs;
            SelectedCategory = CategoryItems.FirstOrDefault();
            Indicator.Hide();
        }

        private VideoCategoryViewModel selectedCategory;
        public VideoCategoryViewModel SelectedCategory
        {
            get { return this.selectedCategory; }
            set
            {
                this.selectedCategory = value;
                this.RaisePropertyChanged(() => this.SelectedCategory);

                if (value.Videos.Count == 0)
                {
                    value.LoadCategoryContent();
                }
            }
        }

        private ObservableCollection<VideoCategoryViewModel> categoryItems;
        public ObservableCollection<VideoCategoryViewModel> CategoryItems
        {
            get
            {
                if (categoryItems == null)
                    categoryItems = new ObservableCollection<VideoCategoryViewModel>();
                return categoryItems;
            }
            set
            {
                this.categoryItems = value;
                this.RaisePropertyChanged(() => this.CategoryItems);
            }
        }

        public void SearchVideo()
        {
            this.NavigationService.Navigate(new Uri("/Views/SearchVideoView.xaml", UriKind.Relative));
        }

        public void Refresh()
        {
            LoadVideosFromService();
        }

        public override void OnPageResumeFromTombstoning()
        {

        }
    }
}
